package homelesspartners.client.widgets;

import homelesspartners.client.model.Gift;
import homelesspartners.client.model.Shelter;
import homelesspartners.client.model.Story;
import homelesspartners.client.model.User;

import java.util.ArrayList;
import java.util.Iterator;

import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;

public class FinishShoppingWidget extends Composite {

	private static final int TABLE_WIDTH = 2;
	private VerticalPanel panel = new VerticalPanel();
	private HTML heading = new HTML("&nbsp;<b>STEP 4 - Instructions</b><br /><br />");
	private HTML subText =
			new HTML(
					"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Thank you for your generosity!  To send these gifts, please follow the instructions below.<br /> <br />");

	private HTML subText2 =
			new HTML(
					"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b>1.&nbsp;&nbsp;Purchase Gift(s)</b><br/><div class='indented'>If possible, give something new rather than used.  Used items are also appreciated, but should be in good condition.  You may include additional unrequested gifts if appropriate, please contact us if you have questions.  <u>Do not give cash.</u></div><br /> <br />");

	private HTML subText3 =
			new HTML(
					"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b>2.&nbsp;&nbsp;Wrap and Label Gift(s)</b><br /><div class='indented'>Please wrap the gift(s) as a Christmas present and <u>label the outside of each gift with the first name, last initial, and ID#</u>, as well as the contents of the package including the size and gender of clothing items.  (If the person cannot be found, the gift will given to another for Christmas).</div><br /> <br />");

	private HTML subText4 =
			new HTML(
					"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b>3.&nbsp;&nbsp;A personal card or message can be as valued as a gift.</b><br/><div class='indented'>If you would like to include one, be sure to place your message in a separate envelope with the first name, last initial, and ID# of the homeless person on the front.  Consider inviting your child to write a caring message as well (you can send messages to as many people as you like).</div><br /> <br />");

	private HTML subText5 =
			new HTML(
					"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b>4.&nbsp;&nbsp;Do not include your name or location information.</b><br/><div class='indented'>If you would like to invite a reply, an email address can be included, as computer access is available at many shelters.</div><br /> <br />");

	private HTML subText6 =
			new HTML(
					"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b>5.&nbsp;&nbsp;Deliver the gift(s) to the shelter(s).</b><br/><div class='indented'>The shelter staff will distribute your gift(s) to the person(s) at Christmas.</div><br /> <br />");

	private HTML subText7 =
			new HTML(
					"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b>6.&nbsp;&nbsp;Shelter Specific Details</b><br/><div class='indented'>There may be more specific instructions for delivering gifts to the shelters you selected, such as hours of operations and alternate drop off locations.  Please see the additional notes for each shelter below.</div><br /> <br />");

	private FlexTable flexTable = new FlexTable();

	private static FinishShoppingWidget instance = new FinishShoppingWidget();

	public static FinishShoppingWidget getInstance() {
		return instance;
	}

	public FinishShoppingWidget() {
		panel.setWidth("720px");
		panel.add(heading);
		panel.add(subText);
		panel.add(subText2);
		panel.add(subText3);
		panel.add(subText4);
		panel.add(subText5);
		panel.add(subText6);
		panel.add(subText7);
		initWidget(panel);
	}

	public void refresh() {
		panel.remove(flexTable);
		doTitle();

		ArrayList shelters = ShoppingListWidget.getInstance().getShoppingList().getShelters();
		for (Iterator iter = shelters.iterator(); iter.hasNext();) {
			Shelter shelter = (Shelter) iter.next();
			doShelter(shelter);
		}
		panel.add(flexTable);

	}

	private void doShelter(Shelter shelter) {
		flexTable.setWidget(maxRow(), 0, new HTML(shelter.getName()));
		flexTable.getFlexCellFormatter().setStylePrimaryName(maxRow() - 1, 0, "sl-shelter");
		mergeLastRow();

		flexTable.setWidget(maxRow(), 0, new HTML("Address"));
		flexTable.getFlexCellFormatter().setStylePrimaryName(maxRow() - 1, 0, "sl-shelter");
		flexTable.setWidget(maxRow() - 1, 1, new Label(shelter.getAddress()));
		flexTable.getFlexCellFormatter().setStylePrimaryName(maxRow() - 1, 1, "sl-text");

		flexTable.setWidget(maxRow(), 0, new HTML("Phone"));
		flexTable.getFlexCellFormatter().setStylePrimaryName(maxRow() - 1, 0, "sl-shelter");
		flexTable.setWidget(maxRow() - 1, 1, new Label(shelter.getPhone()));
		flexTable.getFlexCellFormatter().setStylePrimaryName(maxRow() - 1, 1, "sl-text");

		flexTable.setWidget(maxRow(), 0, new HTML("Shelter Details"));
		flexTable.getFlexCellFormatter().setStylePrimaryName(maxRow() - 1, 0, "sl-shelter");
		if (shelter.getAdditionalDetails() != null) {
			flexTable.setWidget(maxRow() - 1, 1, new HTML(shelter.getAdditionalDetails()));
		} else {
			flexTable.setWidget(maxRow() - 1, 1, new Label("none."));
		}
		flexTable.getFlexCellFormatter().setStylePrimaryName(maxRow() - 1, 1, "sl-text");

		flexTable.setWidget(maxRow(), 0, new HTML("Gifts"));
		flexTable.getFlexCellFormatter().setStylePrimaryName(maxRow() - 1, 0, "sl-shelter");

		FlexTable subTable = new FlexTable();
		subTable.setBorderWidth(1);
		subTable.setStylePrimaryName("sl-gifts");
		subTable.setCellPadding(2);
		subTable.setCellSpacing(2);
		subTable.setWidth("100%");
		flexTable.setWidget(maxRow() - 1, 1, subTable);

		ArrayList stories = shelter.getStories();
		for (Iterator storiesIter = stories.iterator(); storiesIter.hasNext();) {
			Story story = (Story) storiesIter.next();
			Gift[] gifts = story.getGifts();
			int giftCount = 0;
			for(int x = 0; x < gifts.length; x++){
				if (gifts[x] != null && User.getInstance().getEmailAddress().equalsIgnoreCase(gifts[x].getDonor())) {
					giftCount++;
				}
			}
			int maxRow = subTable.getRowCount();
			int firstRow = maxRow;
			subTable.setWidget(maxRow, 0, new Label(story.getHeaderText()));
			subTable.getFlexCellFormatter().setStylePrimaryName(maxRow, 0, "sl-gifts");
			subTable.getFlexCellFormatter().setRowSpan(firstRow, 0, giftCount);
			Gift gift = gifts[0];
			subTable.setWidget(maxRow, 1, new Label(gift.getDescription()));
			subTable.getFlexCellFormatter().setStylePrimaryName(maxRow, 1, "sl-gifts");
			maxRow++;
			for (int x = 1; x < giftCount; maxRow++) {
				gift = gifts[x];
				if (User.getInstance().getEmailAddress().equalsIgnoreCase(gift.getDonor())) {
					subTable.setWidget(maxRow, 0, new Label(gift.getDescription()));
					subTable.getFlexCellFormatter().setStylePrimaryName(maxRow, 0, "sl-gifts");
				}
				x++;
			}

		}

		flexTable.setWidget(maxRow(), 0, new HTML("<br/><br/>"));
		mergeLastRow();
	}

	private void doTitle() {
		flexTable = new FlexTable();
		flexTable.setWidth("720px");
		flexTable.setBorderWidth(1);
		flexTable.setCellPadding(2);
		flexTable.setCellSpacing(0);

		flexTable.setWidget(0, 0, new HTML("Your Shopping List"));
		flexTable.getFlexCellFormatter().setStylePrimaryName(0, 0, "sl-title");
		mergeLastRow();
	}
	private int maxRow() {
		return flexTable.getRowCount();
	}
	private void mergeLastRow() {
		flexTable.getFlexCellFormatter().setColSpan(maxRow() - 1, 0, TABLE_WIDTH);
	}

}
